//////////////////////////////////////////////////////////////////////////////////
//                                                                              //
//  This file is part of the buola project (https://code.google.com/p/buola/).  //
//                                                                              //
//  Copyright(c) 2007-2012 Xavi Gratal                                          //
//  gratal AT gmail DOT com                                                     //
//                                                                              //
//  Buola is free software: you can redistribute it and/or modify               //
//  it under the terms of the GNU General Public License as published by        //
//  the Free Software Foundation, either version 3 of the License, or           //
//  (at your option) any later version.                                         //
//                                                                              //
//  Buola is distributed in the hope that it will be useful,                    //
//  but WITHOUT ANY WARRANTY; without even the implied warranty of              //
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               //
//  GNU General Public License for more details.                                //
//                                                                              //
//  You should have received a copy of the GNU General Public License           //
//  along with buola.  If not, see <http://www.gnu.org/licenses/>.              //
//                                                                              //
//////////////////////////////////////////////////////////////////////////////////

#ifndef _BUOLA_TTY_CSIMPLESHELL_H_
#define _BUOLA_TTY_CSIMPLESHELL_H_

#include <buola/buola.h>
#include <buola/io/curi.h>
#include <buola/tty/ccompletion.h>
#include <buola/tty/clinereader.h>
#include <buola/container/ctrie.h>
#include <map>
#include <functional>

namespace buola { namespace tty {

class CSimpleShell
{
    typedef std::function<void(const std::vector<std::string>&)> TFunction;
    
    struct SCommand
    {
        std::string mName;
        TFunction mFunction;
    };
    
public:
    CSimpleShell();
    
    void SetPrompt(const std::wstring &pPrompt);
    std::wstring GetPrompt();

    void AddCommand(const std::string &pName,const TFunction &pFunction);
    
    void Loop();
    void Stop();

private:
    void ProcessLine(const std::string &pLine);
    bool Complete(const std::wstring &pLine,int pCursor,tty::CCompletion<std::wstring> &pComp);
    
private:
    CTrie<char,std::unique_ptr<SCommand>> mCommands;
    
    std::wstring mPrompt;
    CLineReader mLineReader;
    
    bool mStop;
};

/*namespace tty*/ } /*namespace buola*/ }

#endif
